//
//  TallyListView.m
//  Exam13-2b
//
//  Created by 孙晓晔 on 2017/4/29.
//  Copyright © 2017年 孙晓晔. All rights reserved.
//

#import "TallyListView.h"
#import "TallyListCellModel.h"
#import "TallyViewCell.h"

@interface TallyListView()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong) NSArray *tallyListArray;
@property(nonatomic,assign) CGFloat offsety;
@end

static NSString *reuseIdentifier = @"tallyViewCell";

@implementation TallyListView

-(NSArray *)tallyListArray {
    if (_tallyListArray == nil) {
        NSString *strPath = [[NSBundle mainBundle]pathForResource:@"TallyList.plist" ofType:nil];
        NSArray *arr = [NSArray arrayWithContentsOfFile:strPath];
        
        NSMutableArray *muArr = [NSMutableArray array];
        
        for (NSDictionary *dict in arr) {
            TallyListCellModel *obj = [TallyListCellModel tallyListCellModelWithDict:dict];
            
            [muArr addObject:obj];
        }
        
        _tallyListArray = muArr;
    }
    
    return _tallyListArray;
}

-(instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(nonnull UICollectionViewLayout *)layout {
    
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        self.delegate = self;
        self.dataSource = self;
        self.backgroundColor = [UIColor clearColor];
        UINib *nib = [UINib nibWithNibName:@"TallyListCell" bundle:nil];
        [self registerNib:nib forCellWithReuseIdentifier:reuseIdentifier];
        
    }
    
    return self;
}

#pragma mark UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.tallyListArray count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TallyViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.cellModel = self.tallyListArray[indexPath.row];
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"didSelectItemAtIndexPath", nil);
    
    TallyViewCell *cell = (TallyViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    
    //cell在collectionView的位置
    CGRect cellRect = [collectionView convertRect:cell.frame fromView:collectionView];
    
    //image在cell中的位置
    CGRect imgInCellRect = cell.imageView.frame;
    CGFloat x = cellRect.origin.x + imgInCellRect.origin.x;
    CGFloat y = cellRect.origin.y + imgInCellRect.origin.y + 64 - self.offsety;
    
    //图片在collectionView的位置
    CGRect imgRect = CGRectMake(x, y, imgInCellRect.size.width-10, imgInCellRect.size.height-10);
    
    //回调
    if ([self.tallyDelegate respondsToSelector:@selector(didSelectItem:index:withRectInCollection:)]){
        [self.tallyDelegate didSelectItem:cell index:indexPath.row withRectInCollection:imgRect];
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.offsety = scrollView.contentOffset.y;
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    NSLog(@"scrollViewDidEndDragging", nil);
    
    CGFloat bottomY = self.contentSize.height - self.frame.size.height;
    if (scrollView.contentOffset.y >= bottomY) {
        NSLog(@"计算器下去");
        if ([self.tallyDelegate respondsToSelector:@selector(listScrollToBottom)]) {
            [self.tallyDelegate listScrollToBottom];
        }
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


@end
